package com.ruoyi.report_chl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.DealHeh;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_chl.domain.bo.ChlFmhhslBsInfoBo;
import com.ruoyi.report_chl.domain.vo.ChlFmhhslBsInfoVo;
import com.ruoyi.report_chl.domain.ChlFmhhslBsInfo;
import com.ruoyi.report_chl.mapper.ChlFmhhslBsInfoMapper;
import com.ruoyi.report_chl.service.IChlFmhhslBsInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 粉煤灰含水率Service业务层处理
 *
 * @author zyb
 * @date 2024-07-15
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class ChlFmhhslBsInfoServiceImpl implements IChlFmhhslBsInfoService {

    private final ChlFmhhslBsInfoMapper baseMapper;

    /**
     * 查询粉煤灰含水率
     */
    @Override
    @FIllBasicInfo
    public ChlFmhhslBsInfoVo queryById(Long excelId){
        return baseMapper.selectVoById(excelId);
    }

    /**
     * 查询粉煤灰含水率列表
     */
    @Override
    public TableDataInfo<ChlFmhhslBsInfoVo> queryPageList(ChlFmhhslBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ChlFmhhslBsInfo> lqw = buildQueryWrapper(bo);
        Page<ChlFmhhslBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询粉煤灰含水率列表
     */
    @Override
    public List<ChlFmhhslBsInfoVo> queryList(ChlFmhhslBsInfoBo bo) {
        LambdaQueryWrapper<ChlFmhhslBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ChlFmhhslBsInfo> buildQueryWrapper(ChlFmhhslBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ChlFmhhslBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ChlFmhhslBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ChlFmhhslBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ChlFmhhslBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ChlFmhhslBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ChlFmhhslBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ChlFmhhslBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ChlFmhhslBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ChlFmhhslBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ChlFmhhslBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ChlFmhhslBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), ChlFmhhslBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ChlFmhhslBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ChlFmhhslBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ChlFmhhslBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ChlFmhhslBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ChlFmhhslBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ChlFmhhslBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ChlFmhhslBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ChlFmhhslBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ChlFmhhslBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ChlFmhhslBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getA11Heh()), ChlFmhhslBsInfo::getA11Heh, bo.getA11Heh());
        return lqw;
    }

    /**
     * 新增粉煤灰含水率
     */
    @Override
    public Boolean insertByBo(ChlFmhhslBsInfoBo bo) {
        ChlFmhhslBsInfo add = BeanUtil.toBean(bo, ChlFmhhslBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改粉煤灰含水率
     */
    @Override
    @DealHeh
    public Boolean updateByBo(ChlFmhhslBsInfoBo bo) {
        ChlFmhhslBsInfo update = BeanUtil.toBean(bo, ChlFmhhslBsInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ChlFmhhslBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除粉煤灰含水率
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
